package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.FragmentStylistGuideBinding;
import com.jwdf.uda.event.StylistAnswerEvent;
import com.jwdf.uda.event.StylistDisposeEvent;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.stylist.StylistAnswerFragment;
import com.jwdf.uda.ui.fragment.stylist.StylistConfirmFragment;
import com.jwdf.uda.ui.fragment.stylist.StylistDisposeFragment;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.view.tabstrip.CustomPagerSlidingTabStrip;
import com.jwdf.uda.view.tabstrip.ViewHolder;

/**
 * 类说明：搭配指导服务
 */
public class StylistGuideActivity extends AbsTitleBarActivity {
    public static void startActivity(Context context) {
        Intent intent = new Intent(context, StylistGuideActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected String getTopTitle() {
        return getString(R.string.title_stylist_guide);
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new StylistGuideFragment();
    }

    public static class StylistGuideFragment extends BaseFragment {
        private static final int DISPOSE_TAB_INDEX = 0;
        private static final int CONFIRMED_TAB_INDEX = 1;
        private static final int ANSWER_TAB_INDEX = 2;

        private static final int TAB_COUNT = 3;

        int posSel = 0;
        private StylistDisposeFragment mStylistDisposeFragment;
        private StylistConfirmFragment mStylistConfirmFragment;
        private StylistAnswerFragment mStylistAnswerFragment;

        private FragmentStylistGuideBinding mBinding;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_stylist_guide, container, false);
            initViews();
            return mBinding.getRoot();
        }

        private void initViews() {
            mBinding.vpM.setOffscreenPageLimit(TAB_COUNT);
            FragmentAdapter adapter = new FragmentAdapter(getActivity().getSupportFragmentManager());
            mBinding.vpM.setAdapter(new FragmentAdapter(getActivity().getSupportFragmentManager()));
            adapter.notifyDataSetChanged();
            mBinding.tabs.setViewPager(mBinding.vpM);
            mBinding.tabs.setOnPageChangeListener(mOnPageChangeListener);

            mBinding.vpM.setCurrentItem(0);

            final TextView txt = mBinding.tabs.getTxt(posSel);

            final int size = DisplayUtils.px2sp(getContext(), txt.getTextSize());
            final int unitSP = TypedValue.COMPLEX_UNIT_SP;
            txt.setTextSize(unitSP, size + 1);

            EventBusUtil.registerEvent(this);
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            EventBusUtil.unregisterEvent(this);
        }

        public void onEventMainThread(StylistDisposeEvent event) {
            if (event.getCount() > 0) {
                String dotTxt = "99+";
                if (event.getCount() <= 99) {
                    dotTxt = String.valueOf(event.getCount());
                }
                mBinding.tabs.showDot(0, StringUtils.filterNullAndTrim(dotTxt));
            } else {
                mBinding.tabs.hideDot(0);
            }
        }

        public void onEventMainThread(StylistAnswerEvent event) {
            if(event.isAnswer()){
                mBinding.tabs.showDot(2);
            }else{
                mBinding.tabs.hideDot(2);
            }
        }

        private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(posSel != position) {
                    final TextView txt = mBinding.tabs.getTxt(position);

                    final int size = DisplayUtils.px2sp(getContext(), txt.getTextSize());
                    final int unitSP = TypedValue.COMPLEX_UNIT_SP;
                    mBinding.tabs.getTxt(posSel).setTextSize(unitSP, size);
                    txt.setTextSize(unitSP, size + 1);

                    posSel = position;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        };

        public class FragmentAdapter extends FragmentStatePagerAdapter implements CustomPagerSlidingTabStrip.CustomTabProvider {

            public FragmentAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int position) {
                if (position >= 0 && position < TAB_COUNT) {
                    switch (position) {
                        case DISPOSE_TAB_INDEX:
                            if (null == mStylistDisposeFragment)
                                mStylistDisposeFragment = StylistDisposeFragment.instance();
                            return mStylistDisposeFragment;
                        case CONFIRMED_TAB_INDEX:
                            if (null == mStylistConfirmFragment)
                                mStylistConfirmFragment = StylistConfirmFragment.instance();
                            return mStylistConfirmFragment;
                        case ANSWER_TAB_INDEX:
                            if (null == mStylistAnswerFragment)
                                mStylistAnswerFragment = StylistAnswerFragment.instance();
                            return mStylistAnswerFragment;
                        default:
                            break;
                    }
                }
                return null;
            }

            @Override
            public int getCount() {
                return TAB_COUNT;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                if (position >= 0 && position < TAB_COUNT) {
                    switch (position) {
                        case DISPOSE_TAB_INDEX:
                            return "处理中";
                        case CONFIRMED_TAB_INDEX:
                            return "已确认";
                        case ANSWER_TAB_INDEX:
                            return "待响应";
                        default:
                            break;
                    }
                }
                return null;
            }

            @Override
            public View getSelectTabView(int position, View convertView) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_select_tab, null);
                }

                TextView tv = ViewHolder.get(convertView, R.id.tvTab);

                tv.setText(getPageTitle(position));

                return convertView;
            }

            @Override
            public View getDisSelectTabView(int position, View convertView) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_disselect_tab, null);
                }

                TextView tv = ViewHolder.get(convertView, R.id.tvTab);

                tv.setText(getPageTitle(position));

                return convertView;
            }

        }
    }
}
